package com.xuyiwei.xblog.core.page;

import com.baomidou.mybatisplus.plugins.Page;

import java.util.Collection;
import java.util.List;

/**
 * 分页结果的封装(for Bootstrap Table)
 *
 * @author fengshuonan
 * @Date 2017年1月22日 下午11:06:41
 */
public class PageInfoBT<T> {

    // 结果集
    private List<T> rows;

    // 总数
    private long total;

    //上一页
    private int prePage;

    //下一页
    private int nextPage;

    //一共几页
    private long pages;

    //是不是第一页
    private boolean isFirstPage;

    //是不是最后一页
    private boolean isLastPage;

    //是否含有上一页
    private boolean hasPreviousPage;

    //是否含有下一页
    private boolean hasNextPage;

    //第几页
    private int pageNum;

    //一页几行
    private int pageSize;

    //开始行数 从0开始
    private int startRow;

    //结束行数 从0开始
    private int endRow;

    public PageInfoBT(Page<T> page) {
        this.rows = page.getRecords();
        this.total = page.getTotal();

        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;

        this.pageNum = page.getCurrent();
        this.pageSize = page.getSize();
        this.pages = page.getPages();

        if(page.getRecords().size()==0){
            this.startRow = 0;
            this.endRow = 0;
        }else{
            this.startRow = page.getOffset() + 1;
            this.endRow = this.startRow - 1 + this.pageSize;
        }
        if(this.pageNum<1){
            this.prePage = 0;
        }else{
            this.prePage = this.pageNum -1;
        }
        if(this.pageNum<this.pages){
            this.nextPage = this.pageNum+1;
        }else{
            this.nextPage = (int) this.pages;
        }

        this.judgePageBoudary();

    }

    private void judgePageBoudary() {
        this.isFirstPage = this.pageNum == 1;
        this.isLastPage = this.pageNum == this.pages || this.pages == 0;
        this.hasPreviousPage = this.pageNum > 1;
        this.hasNextPage = this.pageNum < this.pages;
    }


    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getPrePage() {

        return prePage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public long getPages() {
        return pages;
    }

    public void setPages(long pages) {
        this.pages = pages;
    }

    public boolean isFirstPage() {
        return isFirstPage;
    }

    public void setFirstPage(boolean firstPage) {
        isFirstPage = firstPage;
    }

    public boolean isLastPage() {
        return isLastPage;
    }

    public void setLastPage(boolean lastPage) {
        isLastPage = lastPage;
    }

    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public int getEndRow() {
        return endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }
}
